'use strict'
const autoprefixer = require('autoprefixer')
const pxtoviewport = require('postcss-px-to-viewport')
const CompressionWebpackPlugin = require('compression-webpack-plugin')

const path = require('path')
function resolve(dir) {
  return path.join(__dirname, dir)
}

const hcw = 'http://192.168.31.26:8101'
const dfq = 'http://192.168.31.176:8116'
const prod = 'http://uss.xiongmaodangao.com/'
const test = 'http://test.uss.xiongmaodangao.com/'
const dev = 'http://dev.uss.xiongmaodangao.com/'

const defaultSettings = require('./src/settings.js')
const name = defaultSettings.title || '熊猫不走'

module.exports = {
  lintOnSave: true,
  publicPath: './',
  productionSourceMap: process.env.NODE_ENV === 'development', // 去除生产环境.map文件
  configureWebpack: (config) => {
    const configNew = {
      name: name, // 设置 html 标题名
      resolve: {
        // 设置 alias
        alias: {
          '@': resolve('src')
        }
      }
    }
    if (process.env.NODE_ENV === 'production') {
      configNew.plugins = [
        // gzip
        new CompressionWebpackPlugin({
          filename: '[path].gz[query]',
          test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: false
        })
      ]
    } else {
      configNew.devtool = 'eval-source-map'
    }
    return configNew
  },
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          // pxtoviewport({
          //   viewportWidth: 375, // 根据设计稿设定
          //   minPixelValue: 1, // 最小的转换数值
          //   unitPrecision: 2 // 转化精度，转换后保留位数
          // })
        ]
      }
    }
  },
  chainWebpack: (config) => {

    // 设置 svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },
  devServer: {
    port: 8888,
    https: true, // 使用https提供服务
    proxy: {
      '/api': {
        target: dev,
        changeOrigin: true,
        pathRewrite: {
          // '^/api': '/'
        }
      },
    },
  }
}
